package ModelLayer;
import java.util.ArrayList;

/**
 *  A ArrayList container used for storing objects of the class Person
 */
public class AddressBook
{
    private ArrayList<Person> addressBook;
    private static AddressBook instance;
    int index;
    
    /**
     * Constructor for objects of class AddressBook
     */
    public AddressBook()
    {
        addressBook = new ArrayList<Person>();
        index = 0;
    }
    
    public static AddressBook getInstance()
    {
        if(instance == null)
        {
            instance = new AddressBook();
        }
        return instance;
    }
    
    // Adds a person to addressbook 
    public void addPerson(String name, String address, String city, long id, String phone, String zipCode)
    {           
        if (name != null && address != null && city != null && phone != null && zipCode != null)
        {
            Person newPerson = new Person(name, address, city, id, phone, zipCode);
            addressBook.add(newPerson);
        }
    }
    
    // finds a person in addressbook with a search string
    public boolean findPerson(String search)
    {
        index = 0;
        boolean found = false;
            while(index < addressBook.size() && !found)
            {
                Person searchName = addressBook.get(index);
                    if(searchName.getName().equalsIgnoreCase(search))
                    {
                        return found = true;
                    }
                    else
                    {
                        index++;
                    }
                        
            }
        return found = false;
    }
    
    // update each field in a person in addressbook if the input parameter is not null
    public void updatePerson(String search, String name, String address, String city, long id, String phone, String zipCode)
    {
        findPerson(search);
        if (name != null)
        {
            addressBook.get(index).setName(name);
        }
        
        if (address != null)
        {
            addressBook.get(index).setAddress(address);
        }
        
        if (city != null)
        { 
            addressBook.get(index).setCity(city);
        }
        
        // need method to check long id for null
        addressBook.get(index).setID(id);
        
        if (phone != null)
        {
            addressBook.get(index).setPhone(phone);
        }
        
        if (zipCode != null)
        {
            addressBook.get(index).setZipCode(zipCode);
        }
    }
    
    // deletes a specfic person with a search string
    public void deletePerson(String search)
    {
        if (findPerson(search) != false)
        {
            addressBook.remove(index);
        }
    }
    
    // returns a Person Object via a search String for the person's name
    public Person getPerson(String search)
    {
        if (findPerson(search) != false)
        {
            return addressBook.get(index);
        }
        
        return null;
    } 
    
    // Takes all persons' fields in addressbook and returns as a new ArrayList personList
    public ArrayList listAllPersons()
    {
        ArrayList<String> personList = new ArrayList<String>(); 
        for(Person person : addressBook)
        {
            personList.add(" Name: " + person.getName() + "." +
                           " ID: " + Long.toString(person.getID()) + "." +
                           " Address: " + person.getAddress() + "." +
                           " City: " + person.getCity() + "." +
                           " ZipCode: " + person.getZipCode() + "." +
                           " Phone: " + person.getPhone() + "." +
                           System.lineSeparator());
                            
        }
        return personList;
    }
}